var totalPage;
var totalCnt;
var lineInfoData = [];
var branchCompanyData = [];
var chargingStationData = [];
var lastheaderNameEN = undefined;
var deleteFlag;
var updateIndex;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("lineInfoThead", lineInfoTable, search);
	scrollTableSuspend("scrollBody", "lineInfoTable", "lineInfoTable1", "lineInfoThead", "scrollHeight");
	$ajaxList('branchCompany', getBranchCompany);
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	search();
	setTimeout(hideLoading, 500);
})
function resetSearch(){
	formReset('lineInfoSearch');
	$("#changeLine").hide();
	$("#changeSubBranchCompany").hide();
	$("#changeTeam").hide();
	setTimeout(search, 500);
}
function getBranchCompany(data) {
	var length = data.length;
	for (var i = 0;i < length; i++){
    	branchCompanyData = data;
        $("#BranchCompanyID").append("<option value='" + data[i].branchCompanyID + "'>" + data[i].branchCompanyName + "</option>");
    }
}
function changeSubBranchCompany(){
	var branchCompanyID = $("#BranchCompanyID").val();
	if (branchCompanyID) {
		$("#changeSubBranchCompany").show();
		$("#SubBranchCompanyID").empty();
		$("#SubBranchCompanyID").append("<option value=''>请选择</option>");
		$ajaxList('subBranchCompany', getSubBranchCompany, {"branchCompanyID": branchCompanyID});
	} else {
		$("#changeSubBranchCompany").hide();
		$("#SubBranchCompanyID").empty();
		$("#changeTeam").hide();
		$("#TeamID").empty();
	}
}
function getSubBranchCompany(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#SubBranchCompanyID").append("<option value='" + data[i].subBranchCompanyID + "'>" + data[i].subBranchCompanyName + "</option>");
	}
}
function changeCarTeam(){
	var subBranchCompanyID = $("#SubBranchCompanyID").val();
	if (subBranchCompanyID) {
		$("#changeTeam").show();
		$("#TeamID").empty();
		$("#TeamID").append("<option value=''>请选择</option>");
		$ajaxList('carTeam', getCarTeam, {"subBranchCompanyID": subBranchCompanyID});
	} else {
		$("#changeTeam").hide();
		$("#TeamID").empty();
	}
}
function getCarTeam(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#TeamID").append("<option value='" + data[i].teamID + "'>" + data[i].teamName + "</option>");
	}
}
function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
	for (var i = 0; i < length; i++) {
		$("#ChargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function search(headerNameEN) {
	if(lastheaderNameEN != headerNameEN){
		clearOrder(lastheaderNameEN);
		lastheaderNameEN = headerNameEN;
	}
	$ajax('searchLineInfo', 'lineInfoSearch', searchlineInfo);
}
function searchlineInfo(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	lineInfoData = data && data.lineInfoList || [];
	createTableBody("lineInfoTbody", lineInfoTable, lineInfoData);
	setTimeout(hideLoading, 500);
}
function chkBefore(index, chkFlag){
	var checkbox =document.getElementsByName("chk"); 
	for (var i=0; i<checkbox.length; i++) {
		checkbox[i].checked = false; 
	}
	checkbox[index].checked = true; 
	if (chkFlag == "update") {
		update(index);
	} else if(chkFlag == "delete") {
		del();
	} else if(chkFlag == "updatePwd") {
		updatePassword(index);
	}
}
function save() {
	deleteFlag = "save";
	addTitle("lineInfoSave");
	allTable();
}
function update(index) {
	$('#title').html('');
	deleteFlag = "update";
	addTitle("lineInfoUpdate");
	allTable(index);
}
function changeSub(){
	$("#subBranchCompanyID").empty();
	$("#subBranchCompanyID").append('<option value="">请选择</option>');
	$("#subBranchCompanyID").val("");
	var branchCompanyID = $("#branchCompanyID").val();
	if(branchCompanyID){
		$ajaxList('subBranchCompany', getSubBranchCompanyTable, {"branchCompanyID": branchCompanyID});
	}
}
function getSubBranchCompanyTable(data){
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#subBranchCompanyID").append("<option value='" + data[i].subBranchCompanyID + "'>" + data[i].subBranchCompanyName + "</option>");
	}
	if(deleteFlag == "update"){
		$("#subBranchCompanyID").val(lineInfoData[updateIndex].subBranchCompanyID);
	}
	changeTeam();
}
function changeTeam(){
	$("#teamID").empty();
	$("#teamID").append('<option value="">请选择</option>');
	$("#teamID").val("");
	var subBranchCompanyID = $("#subBranchCompanyID").val();
	if(subBranchCompanyID){
		$ajaxList('carTeam', getTeamTable, {"subBranchCompanyID": subBranchCompanyID});
	}
}
function getTeamTable(data){
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#teamID").append("<option value='" + data[i].teamID + "'>" + data[i].teamName + "</option>");
	}
	if(deleteFlag == "update"){
		$("#teamID").val(lineInfoData[updateIndex].teamID);
	}
}
function allTable(index){
	createDetailHead("detailThead", lineInfoTable, 1);
	createDetailBody("detailTbody", lineInfoTable, lineInfoData[index], 1);
	for (var i = 0;i < branchCompanyData.length;i++){
        $("#branchCompanyID").append("<option value='" + branchCompanyData[i].branchCompanyID + "'>" + branchCompanyData[i].branchCompanyName + "</option>");  
	}
	for (var i = 0;i < chargingStationData.length;i++){
        $("#chargingStationID").append("<option value='" + chargingStationData[i].chargingStationID + "'>" + chargingStationData[i].chargingStationName + "</option>");  
	}
	if(deleteFlag == "update") {
		$("#lineID").attr("readonly", true);
		$('#confirm').show();
		$("#branchCompanyID").val(lineInfoData[index].branchCompanyID);
		$("#chargingStationID").val(lineInfoData[index].chargingStationID);
		updateIndex = index;
		changeSub();
		if(lineInfoData[index].enabled == true){
			$("#enabled").val(1);
		}else{
			$("#enabled").val(0);
		}
	} else if(deleteFlag == "save") {
		$("#lineID").attr("readonly", false);
		$('#confirm').show();
	}
	$("#save").modal("show");
}
function confirm(){
	return checkForm("saveForm", lineInfoTable);
}
function getUrl(){
	if(deleteFlag == "save"){
		return 'saveLineInfo';
	} else if(deleteFlag == "update"){
		return 'updateLineInfo';
	} else if(deleteFlag == "delete"){
		return 'deleteLineInfo';
	}
}
function getData(){
	return {lineIDList: getCheckDataList(lineInfoData, "lineID")};
}
function saveLineInfo(data){
	if(deleteFlag == "save"){
	     var result = data && data.result;
	     if (result == "Success") {
	    	 showMessge(messageContent["Insert.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Insert.Failed"]);
	     } else if (result == "LineIDExist"){
	         showMessge(messageContent["LineInfo.LineIDExist"]);
	     } else if (result == "LineNameExist"){
		         showMessge(messageContent["LineInfo.LineNameExist"]);
	     } else {
	    	 showMessge(messageContent["Insert.Error"]);
	     }
	     search();
	}else if(deleteFlag == "update"){
	     var result = data && data.result;
	     if (result == "Success"){
	    	 showMessge(messageContent["Update.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Update.Failed"]);
	     } else if (result == "LineNameExist"){
		         showMessge(messageContent["LineInfo.LineNameExist"]);
	     } else{
	    	 showMessge(messageContent["Update.Error"]);
	     }
	     search();
	}
}
function sure(data){
    var result = data && data.result;
    if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
    search();
}
